package com.alix.paymoney.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.alix.paymoney.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import retrofit2.http.Url;

/**
 * Created by ${吴世芊} on $2016062016/6/6.
 * 浙江答分奇网络科技有限公司
 * 本类功能:
 */

public class ImageUtils {
    public interface LoadListener {
        void onSuccess(Bitmap bitmap);

        void onFail(Exception error);
    }

    public static void load(Context context, String path, ImageView img) {

        try {
            if (!TextUtils.isEmpty(path)){
                Glide.with(context).load(getImageUrl(path)).into(img);
            }
        } catch (Exception e) {
            Loger.e("加载头像失败"+e.toString());
        }
    }
    public static void loadAnimal(Context context, String path, ImageView img) {
        try {
            Glide.with(context)
                    .load(getImageUrl(path))
                    .animate(R.anim.alpha)
                    .into(img);
        } catch (Exception e) {
            Loger.e("加载头像失败"+e.toString());
        }
    }
    public static void loadAnimal(Context context, String path, ImageView img,int errorResId) {
        if (TextUtils.isEmpty(path)){
            img.setImageResource(errorResId);
        }else {
            try {
                Glide.with(context)
                        .load(getImageUrl(path))
                        .animate(R.anim.alpha)
                        .into(img);
            } catch (Exception e) {
                Loger.e("加载头像失败"+e.toString());
            }
        }

    }
    public static void loadError(Context context, String path, ImageView img, int errorResId) {
        if (TextUtils.isEmpty(path)){
            img.setImageResource(errorResId);
        }else {
            try {
                Glide.with(context)
                        .load(getImageUrl(path))
                        .error(errorResId)
                        .crossFade()
                        .into(img);
            } catch (Exception e) {
                Loger.e("图片加载错误" + e.toString());
            }
        }

    }

    public static void loadRoundSize(Activity context, String path, int width,int height,int round,final LoadListener listener){
        Glide.with(context).load(getImageUrl(path))
                .asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                if (listener != null) {
                    listener.onSuccess(resource);
                }
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                if (listener != null) {
                    listener.onFail(e);
                }
            }
        });
    }
    public static void loadRound(Activity context, String path, ImageView imageView, int round, int holder) {
        try {
            Glide.with(context)
                    .load(getImageUrl(path))
                    .error(holder)
                    .transform(new GlideRoundTransform(context, round))
                    .into(imageView);
        } catch (Exception e) {
            Loger.e("加载头像失败"+e.toString());
        }

    }

    public static void loadHolder(Context context, String path, ImageView img, int defaultId) {
        if (TextUtils.isEmpty(path)){
            img.setImageResource(defaultId);
        }else {
            try {
                Glide.with(context)
                        .load(getImageUrl(path))
                        .placeholder(defaultId)
                        .error(defaultId)
                        .crossFade()
                        .into(img);
            } catch (Exception e) {
                Loger.e("加载头像失败"+e.toString());
            }
        }


    }

    public static void loadSize(Context context, String path, ImageView img, int width, int heitht) {

        try {
            Glide.with(context)
                    .load(getImageUrl(path))
                    .override(width, heitht)
                    .crossFade()
                    .into(img);
        } catch (Exception e) {
            Loger.e(e.toString());
        }
    }

    public static void loadCache(Context context, String path, ImageView img) {

        try {
            Glide.with(context)
                    .load(getImageUrl(path))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(img);
        } catch (Exception e) {
            Loger.e(e.toString());
        }
    }

    public static String getImageUrl(String url) {
        if (url.startsWith("http")) {
            return url;
        } else {
            return  url;
        }
    }

    public static void load(Context context, String path, final LoadListener listener) {
        Glide.with(context).load(getImageUrl(path)).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                if (listener != null) {
                    listener.onSuccess(resource);
                }
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                if (listener != null) {
                    listener.onFail(e);
                }
            }
        });
    }

}
